package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.mapper.RecordEnvironmentSterilizeMapper;
import com.lnking.lnkingplace.model.dto.record.environmentSterilize.EnvironmentSterilizeImportDto;
import com.lnking.lnkingplace.model.entity.RecordEnvironmentSterilize;
import com.lnking.lnkingplace.model.enums.DateFormat;
import com.lnking.lnkingplace.model.request.record.environmentSterilize.EnvironmentSterilizeAddRequest;
import com.lnking.lnkingplace.model.request.record.environmentSterilize.EnvironmentSterilizeUpdateRequest;
import com.lnking.lnkingplace.service.IRecordEnvironmentSterilizeService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-29
 */
@Service
public class RecordEnvironmentSterilizeServiceImpl extends CustomServiceImpl<RecordEnvironmentSterilizeMapper, RecordEnvironmentSterilize> implements IRecordEnvironmentSterilizeService {

    @Resource
    IsolationPlaceMapper isolationPlaceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<EnvironmentSterilizeImportDto> result =  ImportUtils.importExcel(file, EnvironmentSterilizeImportDto.class,option);
        result.forEach(r ->{
            if(StringUtils.isNotBlank(r.getPlaceName())){
                RecordEnvironmentSterilize info = new RecordEnvironmentSterilize();
                BeanUtils.copyProperties(r,info);
                //查询placeId
                String placeId = isolationPlaceMapper.getPlaceId(r.getPlaceName());
                Assert.isTrue(StringUtils.isNotBlank(placeId), "该场所不存在："+"【"+r.getPlaceName()+"】");
                info.setPlaceId(placeId);
                info.setCreateTime(LocalDateTime.now());
                if(StringUtils.isNotBlank(r.getSterilizeTime())){
                    info.setSterilizeTime(LocalDateTimeUtil.stringToLocalDateTime(r.getSterilizeTime(), DateFormat.LOCAL_DATE_TIME));
                }
                info.setBuilding(r.getBuilding());
                this.baseMapper.insert(info);
            }
        });
    }

    @Override
    public void add(EnvironmentSterilizeAddRequest params) {
        RecordEnvironmentSterilize info = new RecordEnvironmentSterilize();
        BeanUtils.copyProperties(params,info);
        info.setCreateTime(LocalDateTime.now());
        info.setPlaceId(isolationPlaceMapper.getPlaceId(params.getPlaceName()));
        this.baseMapper.insert(info);
    }

    @Override
    public void update(EnvironmentSterilizeUpdateRequest params) {
        RecordEnvironmentSterilize info = new RecordEnvironmentSterilize();
        BeanUtils.copyProperties(params,info);
        info.setPlaceId(isolationPlaceMapper.getPlaceId(params.getPlaceName()));
        this.baseMapper.updateById(info);
    }
}
